package common;

/**
 * 代理服务器配置
 */
public class ServerConfig {
    // 控制端口 - 接受客户端连接的端口
    private int controlPort;

    // 是否启用身份验证
    private boolean authEnabled;

    // 身份验证令牌
    private String authToken;

    // 最大并发连接数
    private int maxConnections;

    // 代理列表 - 端口映射配置
    private ProxyMapping[] proxyMappings;

    // 是否启用SSL
    private boolean sslEnabled;

    // SSL证书路径
    private String sslCertPath;

    // SSL密钥路径
    private String sslKeyPath;

    // 日志级别
    private String logLevel;

    // 日志文件路径
    private String logPath;

    // Getters and setters

    public int getControlPort() {
        return controlPort;
    }

    public void setControlPort(int controlPort) {
        this.controlPort = controlPort;
    }

    public boolean isAuthEnabled() {
        return authEnabled;
    }

    public void setAuthEnabled(boolean authEnabled) {
        this.authEnabled = authEnabled;
    }

    public String getAuthToken() {
        return authToken;
    }

    public void setAuthToken(String authToken) {
        this.authToken = authToken;
    }

    public int getMaxConnections() {
        return maxConnections;
    }

    public void setMaxConnections(int maxConnections) {
        this.maxConnections = maxConnections;
    }

    public ProxyMapping[] getProxyMappings() {
        return proxyMappings;
    }

    public void setProxyMappings(ProxyMapping[] proxyMappings) {
        this.proxyMappings = proxyMappings;
    }

    public boolean isSslEnabled() {
        return sslEnabled;
    }

    public void setSslEnabled(boolean sslEnabled) {
        this.sslEnabled = sslEnabled;
    }

    public String getSslCertPath() {
        return sslCertPath;
    }

    public void setSslCertPath(String sslCertPath) {
        this.sslCertPath = sslCertPath;
    }

    public String getSslKeyPath() {
        return sslKeyPath;
    }

    public void setSslKeyPath(String sslKeyPath) {
        this.sslKeyPath = sslKeyPath;
    }

    public String getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public String getLogPath() {
        return logPath;
    }

    public void setLogPath(String logPath) {
        this.logPath = logPath;
    }

    /**
     * 从通用配置加载代理服务器配置
     */
    public static ServerConfig fromConfig(Config config) {
        ServerConfig serverConfig = new ServerConfig();

        serverConfig.setControlPort(config.getInt("server.control.port", 7000));
        serverConfig.setAuthEnabled(config.getBoolean("server.auth.enabled", true));
        serverConfig.setAuthToken(config.getString("server.auth.token", "default_token"));
        serverConfig.setMaxConnections(config.getInt("server.max.connections", 1000));
        serverConfig.setSslEnabled(config.getBoolean("server.ssl.enabled", false));
        serverConfig.setSslCertPath(config.getString("server.ssl.cert", "cert.pem"));
        serverConfig.setSslKeyPath(config.getString("server.ssl.key", "key.pem"));
        serverConfig.setLogLevel(config.getString("server.log.level", "INFO"));
        serverConfig.setLogPath(config.getString("server.log.path", "logs/server.log"));

        // 解析代理映射配置
        String mappingsStr = config.getString("server.proxy.mappings", "");
        String[] mappings = mappingsStr.split(",");

        ProxyMapping[] proxyMappings = new ProxyMapping[mappings.length];
        for (int i = 0; i < mappings.length; i++) {
            String mapping = mappings[i].trim();
            if (!mapping.isEmpty()) {
                String[] parts = mapping.split(":");
                if (parts.length >= 3) {
                    ProxyMapping proxyMapping = new ProxyMapping();
                    proxyMapping.setName(parts[0]);
                    proxyMapping.setListenPort(Integer.parseInt(parts[1]));
                    proxyMapping.setProtocol(parts[2]);

                    proxyMappings[i] = proxyMapping;
                }
            }
        }

        serverConfig.setProxyMappings(proxyMappings);

        return serverConfig;
    }
}
